﻿//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Query Application Block
//===============================================================================

using System.Data;
using System.Collections.Generic;

namespace EntLibContrib.Query.Commands
{
	/// <summary>
	/// Simple abstract class to hold the components of a command
	/// </summary>
	public abstract class Command : ICommand
	{
		private ParameterDictionary parameters;
        
        /// <summary>
		/// Initializes a new instance of the <see cref="Command"/> class.
		/// </summary>
		protected Command()
			: this(new ParameterDictionary())
		{
		}

		/// <summary>
		/// Gets the query parameters.
		/// </summary>
		/// <value>A dictionary of parameters.</value>
		public ParameterDictionary Parameters
		{
			get { return parameters; }
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="ServiceCommand"/> class.
		/// </summary>
		/// <param name="parameters">The parameters.</param>
		protected Command(ParameterDictionary parameters)
		{
			if (parameters == null)
				this.parameters = new ParameterDictionary();
			else
				this.parameters = parameters;
		}
		
        /// <summary>
		/// Executes a read action.
		/// </summary>
		/// <returns>
		/// a DataSet containing the results of the read action
		/// </returns>
		public abstract DataSet ExecuteForRead();

		/// <summary>
		/// Executes a write action.
		/// </summary>
		public abstract void ExecuteForWrite();

		/// <summary>
		/// Adds the parameters.
		/// </summary>
		/// <param name="newParameters">The new parameters.</param>
		public virtual void AddParameters(ParameterDictionary newParameters)
		{
			this.parameters.Clear();
			foreach (KeyValuePair<string, IParameter> parameter in newParameters)
			{
				parameters.Add(parameter.Key, parameter.Value);
			}
		}
	}
}
